package com.codemonkey.lq.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * <ul>
 * 	<li>String vs InputStream</li>
 *  <li>String vs OutputStream</li>
 *  <li>String vs byte[]</li>
 *  <li>OutputStream vs InputStream</li>
 * </ul>
 * @author LQ
 *
 */
public class IOUtil {
	private IOUtil() {
		throw new Error("Can not instantize ! ! !");
	}

	// -------------------------------- String vs Byte[] -----------------------------------
	// https://www.cnblogs.com/Free-Thinker/p/6530311.html
	public static byte[] strToByteArray(String str) {
		if (str == null) {
			return null;
		}
		byte[] byteArray = str.getBytes();
		return byteArray;
	}
	
	public static String byteArrayToStr(byte[] byteArray) {
	    if (byteArray == null) {
	        return null;
	    }
	    String str = new String(byteArray);
	    return str;
	}	
	
	public static String byteArrayToHexStr(byte[] byteArray) {
	    if (byteArray == null){
	        return null;
	    }
	    char[] hexArray = "0123456789ABCDEF".toCharArray();
	    char[] hexChars = new char[byteArray.length * 2];
	    for (int j = 0; j < byteArray.length; j++) {
	        int v = byteArray[j] & 0xFF;
	        hexChars[j * 2] = hexArray[v >>> 4];
	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
	    }
	    return new String(hexChars);
	}	

	// -------------------------------- String vs InputStream -----------------------------------

	public static InputStream string2InputStream(String str) {
		//或者
		//ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes());

		InputStream is = new ByteArrayInputStream(str.getBytes());

		return is;
	}

	public static String inputStream2String(InputStream is) {
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		try {
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e.getMessage(), e);
		}
		return buffer.toString();
	}

	//inputStream转String
	public String parse_String(InputStream in) throws Exception {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		int ch;
		while ((ch = in.read()) != -1) {
			swapStream.write(ch);
		}
		return swapStream.toString();
	}

	//String转inputStream
	public ByteArrayInputStream parse_inputStream(String in) throws Exception {
		ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes());
		return input;
	}

	// -------------------------------- OutputStream vs String -----------------------------------

	//OutputStream 转String
	public String parse_String(OutputStream out) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos = (ByteArrayOutputStream) out;
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		return swapStream.toString();
	}

	//String 转outputStream
	public ByteArrayOutputStream parse_outputStream(String in) throws Exception {
		return inputStream2OutStream(parse_inputStream(in));
	}

	// -------------------------------- OutputStream vs InputStream -----------------------------------

	public static OutputStream inputStream2OutputStream(InputStream is) {
		//create temporary bayte array output stream
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		InputStream isFromFirstData = new ByteArrayInputStream(baos.toByteArray());
		return baos;
	}

	public static ByteArrayOutputStream inputStream2OutStream(InputStream in) throws Exception {
		//	 	https://www.cnblogs.com/vigarbuaa/archive/2013/01/13/2858859.html

		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		int ch;
		while ((ch = in.read()) != -1) {
			swapStream.write(ch);
		}
		return swapStream;
	}

	/**
	 * 此方法只适用于文件大小优先的情况下. 下面的outputStream2InputStream2测试失败！
	 * @param bos
	 * @return
	 * @throws IOException
	 */
	public static InputStream outputStream2InputStream(ByteArrayOutputStream bos)
			throws IOException {
		// http://blog.ostermiller.org/convert-java-outputstream-inputstream

		return new ByteArrayInputStream(bos.toByteArray());
	}

	// 失败！
	private static InputStream outputStream2InputStream2(ByteArrayOutputStream bos)
			throws IOException {
		// https://stackoverflow.com/questions/5778658/how-to-convert-outputstream-to-inputstream

		// PipedInputStream_Tests.java

		// take the copy of the stream and re-write it to an InputStream
		PipedInputStream in = new PipedInputStream();
		final PipedOutputStream out = new PipedOutputStream(in);
		// write the original OutputStream to the PipedOutputStream
		bos.writeTo(out);

		return in;
	}

	//outputStream转inputStream
	public ByteArrayInputStream parse(OutputStream out) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos = (ByteArrayOutputStream) out;
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		return swapStream;
	}

}
